package com.swh.dao;

import com.swh.entity.User;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;


/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/21 18:21
 * @Version: 1.0
 */
public class UserDao extends BaseDao {

    public User findByEmail(String email) {
        try {
            return jdbcTemplate.queryForObject("select * from user where email=?", new BeanPropertyRowMapper<>(User.class), email);
        } catch (DataAccessException e) {
//            e.printStackTrace();
            return null;
        }
    }

    public User findByUserName(String username) {
        try {
            return jdbcTemplate.queryForObject("select * from user  where username=?", new BeanPropertyRowMapper<>(User.class), username);
        } catch (DataAccessException e) {
//            e.printStackTrace();
            return null;
        }
    }

    //注册
    public void save(User user) {
        jdbcTemplate.update("insert into user values (null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                user.getUsername(),
                user.getPassword(),
                user.getEmail(),
                user.getQqOpenid(),
                user.getWxOpenid(),
                user.getRealName(),
                user.getAge(),
                user.getPhone(),
                user.getGender(),
                user.getInfo(),
                user.getRegisterTime(),
                user.getLoginTime(),
                user.getPic(),
                user.getLook(),
                user.getIsSecret(),
                user.getDeptName(),
                user.getDeptId());
    }

    //查询所有
    public List<User> findAll() {
        return jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<>(User.class));
    }

    //查询所有带添加查询
    public List<User> search(String realName) {
        return jdbcTemplate.query("select * from user where real_name like ?",
                new BeanPropertyRowMapper<>(User.class), "%" + realName + "%");
    }


    public List<User> findPage(String realName, Integer startIndex, Integer endIndex) {
        return jdbcTemplate.query("select * from user where real_name like ? limit ?,?",
                new BeanPropertyRowMapper<>(User.class), "%" + realName + "%", startIndex, endIndex);
    }

    public Integer countByRealName(String realName) {
        try {
            return jdbcTemplate.queryForObject("select count(1) from user where real_name like ?", Integer.class, "%" + realName + "%");
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public List<Map<String, Object>> findUserByDeptId(Long deptId) {
        String sql = "select id,real_name realName,is_secret isSecret from user where dept_id=?";

        return jdbcTemplate.queryForList(sql, deptId);
    }

    public User findById(Long id) {
        try {
            return jdbcTemplate.queryForObject("select * from user where id=?",
                    new BeanPropertyRowMapper<>(User.class), id);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    //查看数
    public void incLook(Long id) {
        jdbcTemplate.update("update user set look=look+1 where id=?", id);
    }

    //查询粉丝数
    public Integer findFans(Long userId) {
        try {
            return jdbcTemplate.queryForObject("select count(*) from userfocus where user_focus_id=?",
                    Integer.class, userId);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }


    //关注数,根据id查询关注数
    public Integer findFocus(Long id) {
        try {
            return jdbcTemplate.queryForObject("select count(*) from userfocus where user_id=?",
                    Integer.class, id);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    //修改图片地址
    public void updatePic(Long id, String fileName) {
        jdbcTemplate.update("update user set pic=? where id=?", fileName, id);
    }

    public void update(User user) {
        jdbcTemplate.update(
                "update user set password=?,real_name=?,age=?,phone=?,gender=?,info=?,is_secret=?,dept_name=?,dept_id=? where id=?",
                user.getPassword(),
                user.getRealName(),
                user.getAge(),
                user.getPhone(),
                user.getGender(),
                user.getInfo(),
                user.getIsSecret(),
                user.getDeptName(),
                user.getDeptId(),
                user.getId());

    }

    //查看关注那些人
    public List<Long> findFocusListByUserId(Long id) {
        List<Long> focusList = jdbcTemplate.query("select user_focus_id from userfocus where user_id=?", new RowMapper<Long>() {
            @Override
            public Long mapRow(ResultSet resultSet, int i) throws SQLException {
                long user_focus_id = resultSet.getLong("user_focus_id");
                return user_focus_id;
            }
        }, id);
        return focusList;
    }

    public void delFocus(Long userId, Long focusId) {

        jdbcTemplate.update("delete from userfocus where user_id=? and user_focus_id=?", userId, focusId);
    }

    public void addFocus(Long userId, Long focusId) {
        jdbcTemplate.update("insert into userfocus values(?,?)", userId, focusId);
    }

    // 当前页数据
    public List<Map<String, Object>> findFocusPage(Long userId, Integer startIndex, Integer pageSize) {
        String sql = "select u.id,u.real_name realName,u.is_secret isSecret from user u where u.id in \n" +
                "        (   \n" +
                "            select user_focus_id from userfocus where user_id=?         \n" +
                "         ) limit ?,?";
        return jdbcTemplate.queryForList(sql, userId, startIndex, pageSize);
    }

    // 更改用户登录时间
    public void updateLoginTime(Long id) {
        jdbcTemplate.update("update user set login_time=CURDATE() where id=?", id);
    }


    //微信登录
    public User findByWxOpenid(String openid) {
        try {
            return jdbcTemplate.queryForObject("select * from user where wx_openid=?",
                    new BeanPropertyRowMapper<>(User.class), openid);
        } catch (DataAccessException e) {
            return null;
        }
    }
    //QQ登录
    public User findByQqOpenid(String openid) {
        try {
            return jdbcTemplate.queryForObject("select * from user where qq_openid=?",
                    new BeanPropertyRowMapper<>(User.class), openid);
        } catch (DataAccessException e) {
            return null;
        }
    }

    //查询数据库中最大id
    public Long findMaxId() {
        try {
            return jdbcTemplate.queryForObject("select max(id) from user",
                    Long.class);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }


}
